package com.yanqu.road.server.cmd.activity.ppl;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.pb.activity.PplProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ppl.PplModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.manger.activity.ppl.PplMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_ACTIVITY_PPL_USE_GOODS, desc = "泡泡龙使用道具")
public class UseGoodsCmd extends AbstractCommand {

    private int goodsId;
    private int count;

    private UseGoodsResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PplProto.UsePplGoodsReqMsg reqMsg = PplProto.UsePplGoodsReqMsg.parseFrom(packet.getBytes());
        goodsId = reqMsg.getGoodsId();
        count = reqMsg.getCount();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (!PplMgr.isActivityInShowTime()) {
            ret = GameErrorCode.E_ACTIVITY_PPL_NO_IN_TIME;
            return;
        }
        PplModule pplModule = player.getModule(PplModule.class);
        if (!SystemOpenMgr.systemOpen(player, eSystemId.Ppl.getValue())) {
            ret = GameErrorCode.E_ACTIVITY_PPL_SYSTEM_NO_UNLOCK;
            return;
        }
        if (!pplModule.isBeautyChosen()) {
            ret = GameErrorCode.E_ACTIVITY_PPL_BEAUTY_NO_CHOSEN;
            return;
        }

        BagModule bagModule = player.getModule(BagModule.class);
        UserBagItem bagItem = bagModule.getUserBagItem(goodsId);
        if (bagItem == null || bagItem.getNum() <= 0) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
        if (null == goodsInfo) {
            ret = GameErrorCode.E_GOODS_NO_EXIST;
            return;
        }
        if (goodsInfo.getCanUse() <= 0) {
            ret = GameErrorCode.E_BAG_ITEM_CAN_NO_USE;
            return;
        }

        result = pplModule.usePplGoods(goodsId, count);
        ret = result.getRet();
    }

    @Override
    public void sendData(GamePlayer player) {
        PplProto.UsePplGoodsRespMsg.Builder builder = PplProto.UsePplGoodsRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setReward(result.getReward());
        }
        player.sendPacket(ClientProtocol.U_ACTIVITY_PPL_USE_GOODS, builder);
    }
}
